home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / alloc.c < prev    next >
C/C++ Source or Header  |  1992-05-05  |  41KB  |  1,566 lines

  1. /* Storage allocation and gc for GNU Emacs Lisp interpreter.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #ifndef standalone
  24. #include "buffer.h"
  25. #include "window.h"
  26. #endif
  27.  
  28. #define max(A,B) ((A) > (B) ? (A) : (B))
  29.  
  30. /* Macro to verify that storage intended for Lisp objects is not
  31.    out of range to fit in the space for a pointer.
  32.    ADDRESS is the start of the block, and SIZE
  33.    is the amount of space within which objects can start.  */
  34. #define VALIDATE_LISP_STORAGE(address, size)            \
  35. do                                \
  36.   {                                \
  37.     Lisp_Object val;                        \
  38.     XSET (val, Lisp_Cons, (char *) address + size);        \
  39.     if ((char *) XCONS (val) != (char *) address + size)    \
  40.       {                                \
  41.     free (address);                        \
  42.     memory_full ();                        \
  43.       }                                \
  44.   } while (0)
  45.  
  46. /* Number of bytes of consing done since the last gc */
  47. int consing_since_gc;
  48.  
  49. /* Number of bytes of consing since gc before another gc should be done. */
  50. int gc_cons_threshold;
  51.  
  52. /* value of consing_since_gc when undos were last truncated.  */
  53. int consing_at_last_truncate;
  54.  
  55. /* Nonzero during gc */
  56. int gc_in_progress;
  57.  
  58. #ifndef VIRT_ADDR_VARIES
  59. extern
  60. #endif /* VIRT_ADDR_VARIES */
  61.  int malloc_sbrk_used;
  62.  
  63. #ifndef VIRT_ADDR_VARIES
  64. extern
  65. #endif /* VIRT_ADDR_VARIES */
  66.  int malloc_sbrk_unused;
  67.  
  68. /* Two thresholds controlling how much undo information to keep.  */
  69. int undo_threshold;
  70. int undo_high_threshold;
  71.  
  72. /* Non-nil means defun should do purecopy on the function definition */
  73. Lisp_Object Vpurify_flag;
  74.  
  75. /* Argument we give to Fsignal when memory is full.
  76.    Preallocated since perhaps we can't allocate it when memory is full.  */
  77. Lisp_Object memory_exhausted_message;
  78.  
  79. #ifdef AMIGA_DUMP
  80. int *pure;   /* pure array is allocated at run-time */
  81. int puresize = DEF_PURESIZE; /* and has a variable size */
  82. #define PUREBEG (char *) pure
  83. #else /* not AMIGA_DUMP */
  84. #ifndef HAVE_SHM
  85. #ifdef VMS
  86. int pure[PURESIZE / sizeof (int)];    /*no need to initialize - wasted space*/
  87. #else
  88. int pure[PURESIZE / sizeof (int)] = {0,};   /* Force it into data space! */
  89. #endif    /* NOT VMS */
  90. #define PUREBEG (char *) pure
  91. #else
  92. #define pure PURE_SEG_BITS   /* Use shared memory segment */
  93. #define PUREBEG (char *)PURE_SEG_BITS
  94. #endif /* not HAVE_SHM */
  95. #endif /* not AMIGA_DUMP */
  96.  
  97. /* Index in pure at which next pure object will be allocated. */
  98. int pureptr;
  99.  
  100. /* If nonzero, this is a warning delivered by malloc and not yet displayed.  */
  101. char *pending_malloc_warning;
  102.  
  103. Lisp_Object
  104. malloc_warning_1 (str)
  105.      Lisp_Object str;
  106. {
  107.   Fprinc (str, Vstandard_output);
  108.   write_string ("\nKilling some buffers may delay running out of memory.\n", -1);
  109.   write_string ("However, certainly by the time you receive the 95% warning,\n", -1);
  110.   write_string ("you should clean up, kill this Emacs, and start a new one.", -1);
  111.   return Qnil;
  112. }
  113.  
  114. /* malloc calls this if it finds we are near exhausting storage */
  115. malloc_warning (str)
  116.      char *str;
  117. {
  118.   pending_malloc_warning = str;
  119. }
  120.  
  121. display_malloc_warning ()
  122. {
  123.   register Lisp_Object val;
  124.  
  125.   val = build_string (pending_malloc_warning);
  126.   pending_malloc_warning = 0;
  127.   internal_with_output_to_temp_buffer (" *Danger*", malloc_warning_1, val);
  128. }
  129.  
  130. /* Called if malloc returns zero */
  131. memory_full ()
  132. {
  133.   while (1)
  134.     Fsignal (Qerror, memory_exhausted_message);
  135. }
  136.  
  137. /* like malloc and realloc but check for no memory left */
  138.  
  139. long *
  140. xmalloc (size)
  141.      int size;
  142. {
  143.   register long *val;
  144.   /* Avoid failure if malloc (0) returns 0.  */
  145.   if (size == 0)
  146.     size = 1;
  147.   val = (long *) malloc (size);
  148.   if (!val) memory_full ();
  149.   return val;
  150. }
  151.  
  152. long *
  153. xrealloc (block, size)
  154.      long *block;
  155.      int size;
  156. {
  157.   register long *val;
  158.   /* Avoid failure if malloc (0) returns 0.  */
  159.   if (size == 0)
  160.     size = 1;
  161.   val = (long *) realloc (block, size);
  162.   if (!val) memory_full ();
  163.   return val;
  164. }
  165.  
  166. /* Allocation of cons cells */
  167. /* We store cons cells inside of cons_blocks, allocating a new
  168.  cons_block with malloc whenever necessary.  Cons cells reclaimed by
  169.  GC are put on a free list to be reallocated before allocating
  170.  any new cons cells from the latest cons_block.
  171.  
  172.  Each cons_block is just under 1016 bytes long,
  173.  since malloc really allocates in units of powers of two
  174.  and uses 8 bytes for its own overhead. */
  175.  
  176. #define CONS_BLOCK_SIZE \
  177.   ((1016 - sizeof (struct cons_block *)) / sizeof (struct Lisp_Cons))
  178.  
  179. struct cons_block
  180.   {
  181.     struct cons_block *next;
  182.     struct Lisp_Cons conses[CONS_BLOCK_SIZE];
  183.   };
  184.  
  185. struct cons_block *cons_block;
  186. int cons_block_index;
  187.  
  188. struct Lisp_Cons *cons_free_list;
  189.  
  190. void
  191. init_cons ()
  192. {
  193.   cons_block = (struct cons_block *) malloc (sizeof (struct cons_block));
  194.   cons_block->next = 0;
  195.   bzero (cons_block->conses, sizeof cons_block->conses);
  196.   cons_block_index = 0;
  197.   cons_free_list = 0;
  198. }
  199.  
  200. /* Explicitly free a cons cell.  */
  201. free_cons (ptr)
  202.      struct Lisp_Cons *ptr;
  203. {
  204.   XFASTINT (ptr->car) = (int) cons_free_list;
  205.   cons_free_list = ptr;
  206. }
  207.  
  208. DEFUN ("cons", Fcons, Scons, 2, 2, 0,
  209.   "Create a new cons, give it CAR and CDR as components, and return it.")
  210.   (car, cdr)
  211.      Lisp_Object car, cdr;
  212. {
  213.   register Lisp_Object val;
  214.  
  215.   if (cons_free_list)
  216.     {
  217.       XSET (val, Lisp_Cons, cons_free_list);
  218.       cons_free_list = (struct Lisp_Cons *) XFASTINT (cons_free_list->car);
  219.     }
  220.   else
  221.     {
  222.       if (cons_block_index == CONS_BLOCK_SIZE)
  223.     {
  224.       register struct cons_block *new = (struct cons_block *) malloc (sizeof (struct cons_block));
  225.       if (!new) memory_full ();
  226.       VALIDATE_LISP_STORAGE (new, sizeof *new);
  227.       new->next = cons_block;
  228.       cons_block = new;
  229.       cons_block_index = 0;
  230.       XSET (val, Lisp_Cons, &cons_block->conses[CONS_BLOCK_SIZE - 1]);
  231.     }
  232.       XSET (val, Lisp_Cons, &cons_block->conses[cons_block_index++]);
  233.     }
  234.   XCONS (val)->car = car;
  235.   XCONS (val)->cdr = cdr;
  236.   consing_since_gc += sizeof (struct Lisp_Cons);
  237.   return val;
  238. }
  239.  
  240. DEFUN ("list", Flist, Slist, 0, MANY, 0,
  241.   "Return a newly created list whose elements are the arguments (any number).")
  242.   (nargs, args)
  243.      int nargs;
  244.      register Lisp_Object *args;
  245. {
  246.   register Lisp_Object len, val, val_tail;
  247.  
  248.   XFASTINT (len) = nargs;
  249.   val = Fmake_list (len, Qnil);
  250.   val_tail = val;
  251.   while (!NULL (val_tail))
  252.     {
  253.       XCONS (val_tail)->car = *args++;
  254.       val_tail = XCONS (val_tail)->cdr;
  255.     }
  256.   return val;
  257. }
  258.  
  259. DEFUN ("make-list", Fmake_list, Smake_list, 2, 2, 0,
  260.   "Return a newly created list of length LENGTH, with each element being INIT.")
  261.   (length, init)
  262.      register Lisp_Object length, init;
  263. {
  264.   register Lisp_Object val;
  265.   register int size;
  266.  
  267.   if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
  268.     length = wrong_type_argument (Qnatnump, length);
  269.   size = XINT (length);
  270.  
  271.   val = Qnil;
  272.   while (size-- > 0)
  273.     val = Fcons (init, val);
  274.   return val;
  275. }
  276.  
  277. /* Allocation of vectors */
  278.  
  279. struct Lisp_Vector *all_vectors;
  280.  
  281. DEFUN ("make-vector", Fmake_vector, Smake_vector, 2, 2, 0,
  282.   "Return a newly created vector of length LENGTH, with each element being INIT.")
  283.   (length, init)
  284.      register Lisp_Object length, init;
  285. {
  286.   register int sizei, index;
  287.   register Lisp_Object vector;
  288.   register struct Lisp_Vector *p;
  289.  
  290.   if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
  291.     length = wrong_type_argument (Qnatnump, length);
  292.   sizei = XINT (length);
  293.  
  294.   p = (struct Lisp_Vector *) malloc (sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object));
  295.   if (p == 0)
  296.     memory_full ();
  297.   VALIDATE_LISP_STORAGE (p, 0);
  298.  
  299.   XSET (vector, Lisp_Vector, p);
  300.   consing_since_gc += sizeof (struct Lisp_Vector) + (sizei - 1) * sizeof (Lisp_Object);
  301.  
  302.   p->size = sizei;
  303.   p->next = all_vectors;
  304.   all_vectors = p;
  305.  
  306.   for (index = 0; index < sizei; index++)
  307.     p->contents[index] = init;
  308.  
  309.   return vector;
  310. }
  311.  
  312. DEFUN ("vector", Fvector, Svector, 0, MANY, 0,
  313.   "Return a newly created vector with our arguments (any number) as its elements.")
  314.   (nargs, args)
  315.      register int nargs;
  316.      Lisp_Object *args;
  317. {
  318.   register Lisp_Object len, val;
  319.   register int index;
  320.   register struct Lisp_Vector *p;
  321.  
  322.   XFASTINT (len) = nargs;
  323.   val = Fmake_vector (len, Qnil);
  324.   p = XVECTOR (val);
  325.   for (index = 0; index < nargs; index++)
  326.     p->contents[index] = args[index];
  327.   return val;
  328. }
  329.  
  330. /* Allocation of symbols.
  331.  Just like allocation of conses!
  332.  
  333.  Each symbol_block is just under 1016 bytes long,
  334.  since malloc really allocates in units of powers of two
  335.  and uses 8 bytes for its own overhead. */
  336.  
  337. #define SYMBOL_BLOCK_SIZE \
  338.   ((1016 - sizeof (struct symbol_block *)) / sizeof (struct Lisp_Symbol))
  339.  
  340. struct symbol_block
  341.   {
  342.     struct symbol_block *next;
  343.     struct Lisp_Symbol symbols[SYMBOL_BLOCK_SIZE];
  344.   };
  345.  
  346. struct symbol_block *symbol_block;
  347. int symbol_block_index;
  348.  
  349. struct Lisp_Symbol *symbol_free_list;
  350.  
  351. void
  352. init_symbol ()
  353. {
  354.   symbol_block = (struct symbol_block *) malloc (sizeof (struct symbol_block));
  355.   symbol_block->next = 0;
  356.   bzero (symbol_block->symbols, sizeof symbol_block->symbols);
  357.   symbol_block_index = 0;
  358.   symbol_free_list = 0;
  359. }
  360.  
  361. DEFUN ("make-symbol", Fmake_symbol, Smake_symbol, 1, 1, 0,
  362.   "Return a newly allocated uninterned symbol whose name is NAME.\n\
  363. Its value and function definition are void, and its property list is NIL.")
  364.   (str)
  365.      Lisp_Object str;
  366. {
  367.   register Lisp_Object val;
  368.   register struct Lisp_Symbol *p;
  369.  
  370.   CHECK_STRING (str, 0);
  371.  
  372.   if (symbol_free_list)
  373.     {
  374.       XSET (val, Lisp_Symbol, symbol_free_list);
  375.       symbol_free_list
  376.     = (struct Lisp_Symbol *) XFASTINT (symbol_free_list->value);
  377.     }
  378.   else
  379.     {
  380.       if (symbol_block_index == SYMBOL_BLOCK_SIZE)
  381.     {
  382.       struct symbol_block *new = (struct symbol_block *) malloc (sizeof (struct symbol_block));
  383.       if (!new) memory_full ();
  384.       VALIDATE_LISP_STORAGE (new, sizeof *new);
  385.       new->next = symbol_block;
  386.       symbol_block = new;
  387.       symbol_block_index = 0;
  388.     }
  389.       XSET (val, Lisp_Symbol, &symbol_block->symbols[symbol_block_index++]);
  390.     }
  391.   p = XSYMBOL (val);
  392.   p->name = XSTRING (str);
  393.   p->plist = Qnil;
  394.   p->value = Qunbound;
  395.   p->function = Qunbound;
  396.   p->next = 0;
  397.   consing_since_gc += sizeof (struct Lisp_Symbol);
  398.   return val;
  399. }
  400.  
  401. /* Allocation of markers.
  402.  Works like allocation of conses. */
  403.  
  404. #define MARKER_BLOCK_SIZE \
  405.   ((1016 - sizeof (struct marker_block *)) / sizeof (struct Lisp_Marker))
  406.  
  407. struct marker_block
  408.   {
  409.     struct marker_block *next;
  410.     struct Lisp_Marker markers[MARKER_BLOCK_SIZE];
  411.   };
  412.  
  413. struct marker_block *marker_block;
  414. int marker_block_index;
  415.  
  416. struct Lisp_Marker *marker_free_list;
  417.  
  418. void
  419. init_marker ()
  420. {
  421.   marker_block = (struct marker_block *) malloc (sizeof (struct marker_block));
  422.   marker_block->next = 0;
  423.   bzero (marker_block->markers, sizeof marker_block->markers);
  424.   marker_block_index = 0;
  425.   marker_free_list = 0;
  426. }
  427.  
  428. DEFUN ("make-marker", Fmake_marker, Smake_marker, 0, 0, 0,
  429.   "Return a newly allocated marker which does not point at any place.")
  430.   ()
  431. {
  432.   register Lisp_Object val;
  433.   register struct Lisp_Marker *p;
  434.  
  435.   if (marker_free_list)
  436.     {
  437.       XSET (val, Lisp_Marker, marker_free_list);
  438.       marker_free_list
  439.     = (struct Lisp_Marker *) XFASTINT (marker_free_list->chain);
  440.     }
  441.   else
  442.     {
  443.       if (marker_block_index == MARKER_BLOCK_SIZE)
  444.     {
  445.       struct marker_block *new = (struct marker_block *) malloc (sizeof (struct marker_block));
  446.       if (!new) memory_full ();
  447.       VALIDATE_LISP_STORAGE (new, sizeof *new);
  448.       new->next = marker_block;
  449.       marker_block = new;
  450.       marker_block_index = 0;
  451.     }
  452.       XSET (val, Lisp_Marker, &marker_block->markers[marker_block_index++]);
  453.     }
  454.   p = XMARKER (val);
  455.   p->buffer = 0;
  456.   p->bufpos = 0;
  457.   p->chain = Qnil;
  458.   consing_since_gc += sizeof (struct Lisp_Marker);
  459.   return val;
  460. }
  461.  
  462. /* Allocation of strings */
  463.  
  464. /* Strings reside inside of string_blocks.  The entire data of the string,
  465.  both the size and the contents, live in part of the `chars' component of a string_block.
  466.  The `pos' component is the index within `chars' of the first free byte.
  467.  
  468.  first_string_block points to the first string_block ever allocated.
  469.  Each block points to the next one with its `next' field.
  470.  The `prev' fields chain in reverse order.
  471.  The last one allocated is the one currently being filled.
  472.  current_string_block points to it.
  473.  
  474.  The string_blocks that hold individual large strings
  475.  go in a separate chain, started by large_string_blocks.  */
  476.  
  477.  
  478. /* String blocks contain this many useful bytes.
  479.    8184 is power of 2, minus 8 for malloc overhead. */
  480. #define STRING_BLOCK_SIZE (8184 - sizeof (struct string_block_head))
  481.  
  482. /* A string bigger than this gets its own specially-made string block
  483.  if it doesn't fit in the current one. */
  484. #define STRING_BLOCK_OUTSIZE 1024
  485.  
  486. struct string_block_head
  487.   {
  488.     struct string_block *next, *prev;
  489.     int pos;
  490.   };
  491.  
  492. struct string_block
  493.   {
  494.     struct string_block *next, *prev;
  495.     int pos;
  496.     char chars[STRING_BLOCK_SIZE];
  497.   };
  498.  
  499. /* This points to the string block we are now allocating strings.  */
  500.  
  501. struct string_block *current_string_block;
  502.  
  503. /* This points to the oldest string block, the one that starts the chain.  */
  504.  
  505. struct string_block *first_string_block;
  506.  
  507. /* Last string block in chain of those made for individual large strings.  */
  508.  
  509. struct string_block *large_string_blocks;
  510.  
  511. /* If SIZE is the length of a string, this returns how many bytes
  512.    the string occupies in a string_block (including padding).  */
  513.  
  514. #define STRING_FULLSIZE(SIZE)   \
  515. (((SIZE) + 2 * sizeof (int)) & ~(sizeof (int) - 1))
  516.  
  517. void
  518. init_strings ()
  519. {
  520.   current_string_block = (struct string_block *) malloc (sizeof (struct string_block));
  521.   first_string_block = current_string_block;
  522.   consing_since_gc += sizeof (struct string_block);
  523.   current_string_block->next = 0;
  524.   current_string_block->prev = 0;
  525.   current_string_block->pos = 0;
  526.   large_string_blocks = 0;
  527. }
  528.  
  529. static Lisp_Object make_uninit_string ();
  530.  
  531. DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0,
  532.   "Return a newly created string of length LENGTH, with each element being INIT.\n\
  533. Both LENGTH and INIT must be numbers.")
  534.   (length, init)
  535.      Lisp_Object length, init;
  536. {
  537.   register Lisp_Object val;
  538.   register unsigned char *p, *end, c;
  539.  
  540.   if (XTYPE (length) != Lisp_Int || XINT (length) < 0)
  541.     length = wrong_type_argument (Qnatnump, length);
  542.   CHECK_NUMBER (init, 1);
  543.   val = make_uninit_string (XINT (length));
  544.   c = XINT (init);
  545.   p = XSTRING (val)->data;
  546.   end = p + XSTRING (val)->size;
  547.   while (p != end)
  548.     *p++ = c;
  549.   *p = 0;
  550.   return val;
  551. }
  552.  
  553. Lisp_Object
  554. make_string (contents, length)
  555.      char *contents;
  556.      int length;
  557. {
  558.   register Lisp_Object val;
  559.   val = make_uninit_string (length, 0);
  560.   bcopy (contents, XSTRING (val)->data, length);
  561.   return val;
  562. }
  563.  
  564. Lisp_Object
  565. build_string (str)
  566.      char *str;
  567. {
  568.   return make_string (str, strlen (str));
  569. }
  570.  
  571. static Lisp_Object
  572. make_uninit_string (length)
  573.      int length;
  574. {
  575.   register Lisp_Object val;
  576.   register int fullsize = STRING_FULLSIZE (length);
  577.  
  578.   if (length < 0) abort ();
  579.  
  580.   if (fullsize <= STRING_BLOCK_SIZE - current_string_block->pos)
  581.     /* This string can fit in the current string block */
  582.     {
  583.       XSET (val, Lisp_String,
  584.         (struct Lisp_String *) (current_string_block->chars + current_string_block->pos));
  585.       current_string_block->pos += fullsize;
  586.     }
  587.   else if (fullsize > STRING_BLOCK_OUTSIZE)
  588.     /* This string gets its own string block */
  589.     {
  590.       register struct string_block *new
  591.     = (struct string_block *) malloc (sizeof (struct string_block_head) + fullsize);
  592.       if (!new) memory_full ();
  593.       VALIDATE_LISP_STORAGE (new, 0);
  594.       consing_since_gc += sizeof (struct string_block_head) + fullsize;
  595.       new->pos = fullsize;
  596.       new->next = large_string_blocks;
  597.       large_string_blocks = new;
  598.       XSET (val, Lisp_String,
  599.         (struct Lisp_String *) ((struct string_block_head *)new + 1));
  600.     }
  601.   else
  602.     /* Make a new current string block and start it off with this string */
  603.     {
  604.       register struct string_block *new
  605.     = (struct string_block *) malloc (sizeof (struct string_block));
  606.       if (!new) memory_full ();
  607.       VALIDATE_LISP_STORAGE (new, sizeof *new);
  608.       consing_since_gc += sizeof (struct string_block);
  609.       current_string_block->next = new;
  610.       new->prev = current_string_block;
  611.       new->next = 0;
  612.       current_string_block = new;
  613.       new->pos = fullsize;
  614.       XSET (val, Lisp_String,
  615.         (struct Lisp_String *) current_string_block->chars);
  616.     }
  617.     
  618.   XSTRING (val)->size = length;
  619.   XSTRING (val)->data[length] = 0;
  620.  
  621.   return val;
  622. }
  623.  
  624. /* Must get an error if pure storage is full,
  625.  since if it cannot hold a large string
  626.  it may be able to hold conses that point to that string;
  627.  then the string is not protected from gc. */
  628.  
  629. Lisp_Object
  630. make_pure_string (data, length)
  631.      char *data;
  632.      int length;
  633. {
  634.   register Lisp_Object new;
  635.   register int size = sizeof (int) + length + 1;
  636.  
  637.   if (pureptr + size > PURESIZE)
  638.     error ("Pure Lisp storage exhausted");
  639.   XSET (new, Lisp_String, PUREBEG + pureptr);
  640.   XSTRING (new)->size = length;
  641.   bcopy (data, XSTRING (new)->data, length);
  642.   XSTRING (new)->data[length] = 0;
  643.   pureptr += (size + sizeof (int) - 1)
  644.          / sizeof (int) * sizeof (int);
  645.   return new;
  646. }
  647.  
  648. Lisp_Object
  649. pure_cons (car, cdr)
  650.      Lisp_Object car, cdr;
  651. {
  652.   register Lisp_Object new;
  653.  
  654.   if (pureptr + sizeof (struct Lisp_Cons) > PURESIZE)
  655.     error ("Pure Lisp storage exhausted");
  656.   XSET (new, Lisp_Cons, PUREBEG + pureptr);
  657.   pureptr += sizeof (struct Lisp_Cons);
  658.   XCONS (new)->car = Fpurecopy (car);
  659.   XCONS (new)->cdr = Fpurecopy (cdr);
  660.   return new;
  661. }
  662.  
  663. Lisp_Object
  664. make_pure_vector (len)
  665.      int len;
  666. {
  667.   register Lisp_Object new;
  668.   register int size = sizeof (struct Lisp_Vector) + (len - 1) * sizeof (Lisp_Object);
  669.  
  670.   if (pureptr + size > PURESIZE)
  671.     error ("Pure Lisp storage exhausted");
  672.  
  673.   XSET (new, Lisp_Vector, PUREBEG + pureptr);
  674.   pureptr += size;
  675.   XVECTOR (new)->size = len;
  676.   return new;
  677. }
  678.  
  679. DEFUN ("purecopy", Fpurecopy, Spurecopy, 1, 1, 0,
  680.   "Make a copy of OBJECT in pure storage.\n\
  681. Recursively copies contents of vectors and cons cells.\n\
  682. Does not copy symbols.")
  683.   (obj)
  684.      register Lisp_Object obj;
  685. {
  686.   register Lisp_Object new, tem;
  687.   register int i;
  688.  
  689.   if (NULL (Vpurify_flag))
  690.     return obj;
  691.  
  692.   if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
  693.       && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
  694.     return obj;
  695.  
  696. #ifdef SWITCH_ENUM_BUG
  697.   switch ((int) XTYPE (obj))
  698. #else
  699.   switch (XTYPE (obj))
  700. #endif
  701.     {
  702.     case Lisp_Marker:
  703.       error ("Attempt to copy a marker to pure storage");
  704.  
  705.     case Lisp_Cons:
  706.       return pure_cons (XCONS (obj)->car, XCONS (obj)->cdr);
  707.  
  708.     case Lisp_String:
  709.       return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size);
  710.  
  711.     case Lisp_Vector:
  712.       new = make_pure_vector (XVECTOR (obj)->size);
  713.       for (i = 0; i < XVECTOR (obj)->size; i++)
  714.     {
  715.       tem = XVECTOR (obj)->contents[i];
  716.       XVECTOR (new)->contents[i] = Fpurecopy (tem);
  717.     }
  718.       return new;
  719.  
  720.     default:
  721.       return obj;
  722.     }
  723. }
  724.  
  725. /* Recording what needs to be marked for gc.  */
  726.  
  727. struct gcpro *gcprolist;
  728.  
  729. #define NSTATICS 200
  730.  
  731. int staticidx = 0;
  732.  
  733. #ifdef __GNUC__
  734. Lisp_Object *staticvec[NSTATICS] = {0};
  735. #else
  736. #ifdef AMIGA_DUMP
  737. Lisp_Object *staticvec[NSTATICS]; /* Doesn't need to be pure */
  738. #else
  739. char staticvec1[NSTATICS * sizeof (Lisp_Object *)] = {0};
  740. #define staticvec ((Lisp_Object **) staticvec1)
  741. #endif
  742. #endif
  743.  
  744. /* Put an entry in staticvec, pointing at the variable whose address is given */
  745.  
  746. void
  747. staticpro (varaddress)
  748.      Lisp_Object *varaddress;
  749. {
  750.   staticvec[staticidx++] = varaddress;
  751.   if (staticidx >= NSTATICS)
  752.     abort ();
  753. }
  754.  
  755. struct catchtag
  756.   {
  757.     Lisp_Object tag;
  758.     Lisp_Object val;
  759.     struct catchtag *next;
  760. /*    jmp_buf jmp;  /* We don't need this for GC purposes */
  761.   };
  762.  
  763. extern struct catchtag *catchlist;
  764.  
  765. struct backtrace
  766.   {
  767.     struct backtrace *next;
  768.     Lisp_Object *function;
  769.     Lisp_Object *args;    /* Points to vector of args. */
  770.     int nargs;        /* length of vector */
  771.            /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
  772.     char evalargs;
  773.   };
  774.  
  775. extern struct backtrace *backtrace_list;
  776.  
  777. /* Two flags that are set during GC in the `size' component
  778.    of a string or vector.  On some machines, these flags
  779.    are defined by the m- file to be different bits.  */
  780.  
  781. /* On vector, means it has been marked.
  782.    On string size field or a reference to a string,
  783.    means not the last reference in the chain.  */
  784.  
  785. #ifndef ARRAY_MARK_FLAG
  786. #define ARRAY_MARK_FLAG ((MARKBIT >> 1) & ~MARKBIT)
  787. #endif /* no ARRAY_MARK_FLAG */
  788.  
  789. /* Any slot that is a Lisp_Object can point to a string
  790.    and thus can be put on a string's reference-chain
  791.    and thus may need to have its ARRAY_MARK_FLAG set.
  792.    This includes the slots whose markbits are used to mark
  793.    the containing objects.  */
  794.  
  795. #if ARRAY_MARK_FLAG == MARKBIT
  796. you lose
  797. #endif
  798.  
  799. int total_conses, total_markers, total_symbols, total_string_size, total_vector_size;
  800. int total_free_conses, total_free_markers, total_free_symbols;
  801.  
  802. static void mark_object (), mark_buffer ();
  803. static void clear_marks (), gc_sweep ();
  804. static void compact_strings ();
  805.  
  806. DEFUN ("garbage-collect", Fgarbage_collect, Sgarbage_collect, 0, 0, "",
  807.   "Reclaim storage for Lisp objects no longer needed.\n\
  808. Returns info on amount of space in use:\n\
  809.  ((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)\n\
  810.   (USED-MARKERS . FREE-MARKERS) USED-STRING-CHARS USED-VECTOR-SLOTS)\n\
  811. Garbage collection happens automatically if you cons more than\n\
  812. gc-cons-threshold  bytes of Lisp data since previous garbage collection.")
  813.   ()
  814. {
  815.   register struct gcpro *tail;
  816.   register struct specbinding *bind;
  817.   struct catchtag *catch;
  818.   struct handler *handler;
  819.   register struct backtrace *backlist;
  820.   register Lisp_Object tem;
  821.   char *omessage = echo_area_contents;
  822.  
  823.   register int i;
  824.  
  825.   if (!noninteractive)
  826.     message1 ("Garbage collecting...");
  827.  
  828.   /* Don't keep command history around forever */
  829.   tem = Fnthcdr (make_number (30), Vcommand_history);
  830.   if (CONSP (tem))
  831.     XCONS (tem)->cdr = Qnil;
  832.   /* Likewise for undo information.  */
  833.   truncate_all_undos ();
  834.  
  835.   gc_in_progress = 1;
  836.  
  837. /*  clear_marks ();  */
  838.  
  839.   /* In each "large string", set the MARKBIT of the size field.
  840.      That enables mark_object to recognize them.  */
  841.   {
  842.     register struct string_block *b;
  843.     for (b = large_string_blocks; b; b = b->next)
  844.       ((struct Lisp_String *)(&b->chars[0]))->size |= MARKBIT;
  845.   }
  846.  
  847.   /* Mark all the special slots that serve as the roots of accessibility.
  848.  
  849.      Usually the special slots to mark are contained in particular structures.
  850.      Then we know no slot is marked twice because the structures don't overlap.
  851.      In some cases, the structures point to the slots to be marked.
  852.      For these, we use MARKBIT to avoid double marking of the slot.  */
  853.  
  854.   for (i = 0; i < staticidx; i++)
  855.     mark_object (staticvec[i]);
  856.   for (tail = gcprolist; tail; tail = tail->next)
  857.     for (i = 0; i < tail->nvars; i++)
  858.       if (!XMARKBIT (tail->var[i]))
  859.     {
  860.       mark_object (&tail->var[i]);
  861.       XMARK (tail->var[i]);
  862.     }
  863.   for (bind = specpdl; bind != specpdl_ptr; bind++)
  864.     {
  865.       mark_object (&bind->symbol);
  866.       mark_object (&bind->old_value);
  867.     }
  868.   for (catch = catchlist; catch; catch = catch->next)
  869.     {
  870.       mark_object (&catch->tag);
  871.       mark_object (&catch->val);
  872.     }  
  873.   for (handler = handlerlist; handler; handler = handler->next)
  874.     {
  875.       mark_object (&handler->handler);
  876.       mark_object (&handler->var);
  877.     }  
  878.   for (backlist = backtrace_list; backlist; backlist = backlist->next)
  879.     {
  880.       if (!XMARKBIT (*backlist->function))
  881.     {
  882.       mark_object (backlist->function);
  883.       XMARK (*backlist->function);
  884.     }
  885.       if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
  886.     i = 0;
  887.       else
  888.     i = backlist->nargs - 1;
  889.       for (; i >= 0; i--)
  890.     if (!XMARKBIT (backlist->args[i]))
  891.       {
  892.         mark_object (&backlist->args[i]);
  893.         XMARK (backlist->args[i]);
  894.       }
  895.     }  
  896.  
  897.   gc_sweep ();
  898.  
  899.   /* Clear the mark bits that we set in certain root slots.  */
  900.  
  901.   for (tail = gcprolist; tail; tail = tail->next)
  902.     for (i = 0; i < tail->nvars; i++)
  903.       XUNMARK (tail->var[i]);
  904.   for (backlist = backtrace_list; backlist; backlist = backlist->next)
  905.     {
  906.       XUNMARK (*backlist->function);
  907.       if (backlist->nargs == UNEVALLED || backlist->nargs == MANY)
  908.     i = 0;
  909.       else
  910.     i = backlist->nargs - 1;
  911.       for (; i >= 0; i--)
  912.     XUNMARK (backlist->args[i]);
  913.     }  
  914.   XUNMARK (buffer_defaults.name);
  915.   XUNMARK (buffer_local_symbols.name);
  916.  
  917. /*  clear_marks (); */
  918.   gc_in_progress = 0;
  919.  
  920.   consing_since_gc = 0;
  921.   consing_at_last_truncate = 0;
  922.   if (gc_cons_threshold < 10000)
  923.     gc_cons_threshold = 10000;
  924.  
  925.   if (omessage)
  926.     message1 (omessage);
  927.   else if (!noninteractive)
  928.     message1 ("Garbage collecting...done");
  929.  
  930.   return Fcons (Fcons (make_number (total_conses),
  931.                make_number (total_free_conses)),
  932.         Fcons (Fcons (make_number (total_symbols),
  933.                   make_number (total_free_symbols)),
  934.                Fcons (Fcons (make_number (total_markers),
  935.                      make_number (total_free_markers)),
  936.                   Fcons (make_number (total_string_size),
  937.                      Fcons (make_number (total_vector_size),
  938.                         Qnil)))));
  939. }
  940.  
  941. #if 0
  942. static void
  943. clear_marks ()
  944. {
  945.   /* Clear marks on all conses */
  946.   {
  947.     register struct cons_block *cblk;
  948.     register int lim = cons_block_index;
  949.   
  950.     for (cblk = cons_block; cblk; cblk = cblk->next)
  951.       {
  952.     register int i;
  953.     for (i = 0; i < lim; i++)
  954.       XUNMARK (cblk->conses[i].car);
  955.     lim = CONS_BLOCK_SIZE;
  956.       }
  957.   }
  958.   /* Clear marks on all symbols */
  959.   {
  960.     register struct symbol_block *sblk;
  961.     register int lim = symbol_block_index;
  962.   
  963.     for (sblk = symbol_block; sblk; sblk = sblk->next)
  964.       {
  965.     register int i;
  966.     for (i = 0; i < lim; i++)
  967.       {
  968.         XUNMARK (sblk->symbols[i].plist);
  969.       }
  970.     lim = SYMBOL_BLOCK_SIZE;
  971.       }
  972.   }
  973.   /* Clear marks on all markers */
  974.   {
  975.     register struct marker_block *sblk;
  976.     register int lim = marker_block_index;
  977.   
  978.     for (sblk = marker_block; sblk; sblk = sblk->next)
  979.       {
  980.     register int i;
  981.     for (i = 0; i < lim; i++)
  982.       XUNMARK (sblk->markers[i].chain);
  983.     lim = MARKER_BLOCK_SIZE;
  984.       }
  985.   }
  986.   /* Clear mark bits on all buffers */
  987.   {
  988.     register struct buffer *nextb = all_buffers;
  989.  
  990.     while (nextb)
  991.       {
  992.     XUNMARK (nextb->name);
  993.     nextb = nextb->next;
  994.       }
  995.   }
  996. }
  997. #endif
  998.  
  999. /* Mark reference to a Lisp_Object.  If the object referred to
  1000.    has not been seen yet, recursively mark all the references contained in it.
  1001.  
  1002.    If the object referenced is a short string, the referrencing slot
  1003.    is threaded into a chain of such slots, pointed to from
  1004.    the `size' field of the string.  The actual string size
  1005.    lives in the last slot in the chain.  We recognize the end
  1006.    because it is < (unsigned) STRING_BLOCK_SIZE.  */
  1007.  
  1008. static void
  1009. mark_object (objptr)
  1010.      Lisp_Object *objptr;
  1011. {
  1012.   register Lisp_Object obj;
  1013.  
  1014.   obj = *objptr;
  1015.   XUNMARK (obj);
  1016.  
  1017.  loop:
  1018.  
  1019.   if ((PNTR_COMPARISON_TYPE) XPNTR (obj) < (PNTR_COMPARISON_TYPE) ((char *) pure + PURESIZE)
  1020.       && (PNTR_COMPARISON_TYPE) XPNTR (obj) >= (PNTR_COMPARISON_TYPE) pure)
  1021.     return;
  1022.  
  1023. #ifdef SWITCH_ENUM_BUG
  1024.   switch ((int) XGCTYPE (obj))
  1025. #else
  1026.   switch (XGCTYPE (obj))
  1027. #endif
  1028.     {
  1029.     case Lisp_String:
  1030.       {
  1031.     register struct Lisp_String *ptr = XSTRING (obj);
  1032.  
  1033.     if (ptr->size & MARKBIT)
  1034.       /* A large string.  Just set ARRAY_MARK_FLAG.  */
  1035.       ptr->size |= ARRAY_MARK_FLAG;
  1036.     else
  1037.       {
  1038.         /* A small string.  Put this reference
  1039.            into the chain of references to it.
  1040.            The address OBJPTR is even, so if the address
  1041.            includes MARKBIT, put it in the low bit
  1042.            when we store OBJPTR into the size field.  */
  1043.  
  1044.         if (XMARKBIT (*objptr))
  1045.           {
  1046.         XFASTINT (*objptr) = ptr->size;
  1047.         XMARK (*objptr);
  1048.           }
  1049.         else
  1050.           XFASTINT (*objptr) = ptr->size;
  1051.         if ((int)objptr & 1) abort ();
  1052.         ptr->size = (int) objptr & ~MARKBIT;
  1053.         if ((int) objptr & MARKBIT)
  1054.           ptr->size ++;
  1055.       }
  1056.       }
  1057.       break;
  1058.  
  1059.     case Lisp_Vector:
  1060.     case Lisp_Window:
  1061.     case Lisp_Process:
  1062.     case Lisp_Window_Configuration:
  1063.       {
  1064.     register struct Lisp_Vector *ptr = XVECTOR (obj);
  1065.     register int size = ptr->size;
  1066.     register int i;
  1067.  
  1068.     if (size & ARRAY_MARK_FLAG) break;   /* Already marked */
  1069.     ptr->size |= ARRAY_MARK_FLAG; /* Else mark it */
  1070.     for (i = 0; i < size; i++)     /* and then mark its elements */
  1071.       mark_object (&ptr->contents[i]);
  1072.       }
  1073.       break;
  1074.  
  1075. #if 0
  1076.     case Lisp_Temp_Vector:
  1077.       {
  1078.     register struct Lisp_Vector *ptr = XVECTOR (obj);
  1079.     register int size = ptr->size;
  1080.     register int i;
  1081.  
  1082.     for (i = 0; i < size; i++)     /* and then mark its elements */
  1083.       mark_object (&ptr->contents[i]);
  1084.       }
  1085.       break;
  1086. #endif 0
  1087.  
  1088.     case Lisp_Symbol:
  1089.       {
  1090.     register struct Lisp_Symbol *ptr = XSYMBOL (obj);
  1091.     struct Lisp_Symbol *ptrx;
  1092.  
  1093.     if (XMARKBIT (ptr->plist)) break;
  1094.     XMARK (ptr->plist);
  1095.     XSETTYPE (*(Lisp_Object *) &ptr->name, Lisp_String);
  1096.     mark_object (&ptr->name);
  1097.     mark_object ((Lisp_Object *) &ptr->value);
  1098.     mark_object (&ptr->function);
  1099.     mark_object (&ptr->plist);
  1100.     ptr = ptr->next;
  1101.     if (ptr)
  1102.       {
  1103.         ptrx = ptr;        /* Use pf ptrx avoids compiler bug on Sun */
  1104.         XSETSYMBOL (obj, ptrx);
  1105.         goto loop;
  1106.       }
  1107.       }
  1108.       break;
  1109.  
  1110.     case Lisp_Marker:
  1111.       XMARK (XMARKER (obj)->chain);
  1112.       /* DO NOT mark thru the marker's chain.
  1113.      The buffer's markers chain does not preserve markers from gc;
  1114.      instead, markers are removed from the chain when they are freed by gc. */
  1115.       break;
  1116.  
  1117.     case Lisp_Cons:
  1118.     case Lisp_Buffer_Local_Value:
  1119.     case Lisp_Some_Buffer_Local_Value:
  1120.       {
  1121.     register struct Lisp_Cons *ptr = XCONS (obj);
  1122.     if (XMARKBIT (ptr->car)) break;
  1123.     XMARK (ptr->car);
  1124.     mark_object (&ptr->car);
  1125.     objptr = &ptr->cdr;
  1126.     obj = ptr->cdr;
  1127.     goto loop;
  1128.       }
  1129.  
  1130.     case Lisp_Buffer:
  1131.       if (!XMARKBIT (XBUFFER (obj)->name))
  1132.     mark_buffer (obj);
  1133.       break;
  1134.  
  1135.     case Lisp_Int:
  1136.     case Lisp_Void:
  1137.     case Lisp_Subr:
  1138.     case Lisp_Intfwd:
  1139.     case Lisp_Boolfwd:
  1140.     case Lisp_Objfwd:
  1141.     case Lisp_Buffer_Objfwd:
  1142.     case Lisp_Internal_Stream:
  1143.     /* Don't bother with Lisp_Buffer_Objfwd,
  1144.        since all markable slots in current buffer marked anyway.  */
  1145.     /* Don't need to do Lisp_Objfwd, since the places they point
  1146.        are protected with staticpro.  */
  1147.       break;
  1148.  
  1149.     default:
  1150.       abort ();
  1151.     }
  1152. }
  1153.  
  1154. /* Mark the pointers in a buffer structure.  */
  1155.  
  1156. static void
  1157. mark_buffer (buf)
  1158.      Lisp_Object buf;
  1159. {
  1160.   Lisp_Object tem;
  1161.   register struct buffer *buffer = XBUFFER (buf);
  1162.   register Lisp_Object *ptr;
  1163.  
  1164.   /* This is the buffer's markbit */
  1165.   mark_object (&buffer->name);
  1166.   XMARK (buffer->name);
  1167.  
  1168.   for (ptr = &buffer->name + 1;
  1169.        (char *)ptr < (char *)buffer + sizeof (struct buffer);
  1170.        ptr++)
  1171.     mark_object (ptr);
  1172. }
  1173.  
  1174. /* Find all structures not marked, and free them. */
  1175.  
  1176. static void
  1177. gc_sweep ()
  1178. {
  1179.   total_string_size = 0;
  1180.   compact_strings ();
  1181.  
  1182.   /* Put all unmarked conses on free list */
  1183.   {
  1184.     register struct cons_block *cblk;
  1185.     register int lim = cons_block_index;
  1186.     register int num_free = 0, num_used = 0;
  1187.  
  1188.     cons_free_list = 0;
  1189.   
  1190.     for (cblk = cons_block; cblk; cblk = cblk->next)
  1191.       {
  1192.     register int i;
  1193.     for (i = 0; i < lim; i++)
  1194.       if (!XMARKBIT (cblk->conses[i].car))
  1195.         {
  1196.           XFASTINT (cblk->conses[i].car) = (int) cons_free_list;
  1197.           num_free++;
  1198.           cons_free_list = &cblk->conses[i];
  1199.         }
  1200.       else
  1201.         {
  1202.           num_used++;
  1203.           XUNMARK (cblk->conses[i].car);
  1204.         }
  1205.     lim = CONS_BLOCK_SIZE;
  1206.       }
  1207.     total_conses = num_used;
  1208.     total_free_conses = num_free;
  1209.   }
  1210.  
  1211.   /* Put all unmarked symbols on free list */
  1212.   {
  1213.     register struct symbol_block *sblk;
  1214.     register int lim = symbol_block_index;
  1215.     register int num_free = 0, num_used = 0;
  1216.  
  1217.     symbol_free_list = 0;
  1218.   
  1219.     for (sblk = symbol_block; sblk; sblk = sblk->next)
  1220.       {
  1221.     register int i;
  1222.     for (i = 0; i < lim; i++)
  1223.       if (!XMARKBIT (sblk->symbols[i].plist))
  1224.         {
  1225.           XFASTINT (sblk->symbols[i].value) = (int) symbol_free_list;
  1226.           symbol_free_list = &sblk->symbols[i];
  1227.           num_free++;
  1228.         }
  1229.       else
  1230.         {
  1231.           num_used++;
  1232.           sblk->symbols[i].name
  1233.         = XSTRING (*(Lisp_Object *) &sblk->symbols[i].name);
  1234.           XUNMARK (sblk->symbols[i].plist);
  1235.         }
  1236.     lim = SYMBOL_BLOCK_SIZE;
  1237.       }
  1238.     total_symbols = num_used;
  1239.     total_free_symbols = num_free;
  1240.   }
  1241.  
  1242. #ifndef standalone
  1243.   /* Put all unmarked markers on free list.
  1244.      Dechain each one first from the buffer it points into. */
  1245.   {
  1246.     register struct marker_block *mblk;
  1247.     struct Lisp_Marker *tem1;
  1248.     register int lim = marker_block_index;
  1249.     register int num_free = 0, num_used = 0;
  1250.  
  1251.     marker_free_list = 0;
  1252.   
  1253.     for (mblk = marker_block; mblk; mblk = mblk->next)
  1254.       {
  1255.     register int i;
  1256.     for (i = 0; i < lim; i++)
  1257.       if (!XMARKBIT (mblk->markers[i].chain))
  1258.         {
  1259.           Lisp_Object tem;
  1260.           tem1 = &mblk->markers[i];  /* tem1 avoids Sun compiler bug */
  1261.           XSET (tem, Lisp_Marker, tem1);
  1262.           if (tem1->buffer)
  1263.         unchain_marker (tem);
  1264.           XFASTINT (mblk->markers[i].chain) = (int) marker_free_list;
  1265.           marker_free_list = &mblk->markers[i];
  1266.           num_free++;
  1267.         }
  1268.       else
  1269.         {
  1270.           num_used++;
  1271.           XUNMARK (mblk->markers[i].chain);
  1272.         }
  1273.     lim = MARKER_BLOCK_SIZE;
  1274.       }
  1275.  
  1276.     total_markers = num_used;
  1277.     total_free_markers = num_free;
  1278.   }
  1279.  
  1280.   /* Free all unmarked buffers */
  1281.   {
  1282.     register struct buffer *buffer = all_buffers, *prev = 0, *next;
  1283.  
  1284.     while (buffer)
  1285.       if (!XMARKBIT (buffer->name))
  1286.     {
  1287.       if (prev)
  1288.         prev->next = buffer->next;
  1289.       else
  1290.         all_buffers = buffer->next;
  1291.       next = buffer->next;
  1292.       free (buffer);
  1293.       buffer = next;
  1294.     }
  1295.       else
  1296.     {
  1297.       XUNMARK (buffer->name);
  1298.       prev = buffer, buffer = buffer->next;
  1299.     }
  1300.   }
  1301.  
  1302. #endif standalone
  1303.  
  1304.   /* Free all unmarked vectors */
  1305.   {
  1306.     register struct Lisp_Vector *vector = all_vectors, *prev = 0, *next;
  1307.     total_vector_size = 0;
  1308.  
  1309.     while (vector)
  1310.       if (!(vector->size & ARRAY_MARK_FLAG))
  1311.     {
  1312.       if (prev)
  1313.         prev->next = vector->next;
  1314.       else
  1315.         all_vectors = vector->next;
  1316.       next = vector->next;
  1317.       free (vector);
  1318.       vector = next;
  1319.     }
  1320.       else
  1321.     {
  1322.       vector->size &= ~ARRAY_MARK_FLAG;
  1323.       total_vector_size += vector->size;
  1324.       prev = vector, vector = vector->next;
  1325.     }
  1326.   }
  1327.  
  1328.   /* Free all "large strings" not marked with ARRAY_MARK_FLAG.  */
  1329.   {
  1330.     register struct string_block *sb = large_string_blocks, *prev = 0, *next;
  1331.  
  1332.     while (sb)
  1333.       if (!(((struct Lisp_String *)(&sb->chars[0]))->size & ARRAY_MARK_FLAG))
  1334.     {
  1335.       if (prev)
  1336.         prev->next = sb->next;
  1337.       else
  1338.         large_string_blocks = sb->next;
  1339.       next = sb->next;
  1340.       free (sb);
  1341.       sb = next;
  1342.     }
  1343.       else
  1344.     {
  1345.       ((struct Lisp_String *)(&sb->chars[0]))->size
  1346.         &= ~ARRAY_MARK_FLAG & ~MARKBIT;
  1347.       total_string_size += ((struct Lisp_String *)(&sb->chars[0]))->size;
  1348.       prev = sb, sb = sb->next;
  1349.     }
  1350.   }
  1351. }
  1352.  
  1353. /* Compactify strings, relocate references to them, and
  1354.    free any string blocks that become empty.  */
  1355.  
  1356. static void
  1357. compact_strings ()
  1358. {
  1359.   /* String block of old strings we are scanning.  */
  1360.   register struct string_block *from_sb;
  1361.   /* A preceding string block (or maybe the same one)
  1362.      where we are copying the still-live strings to.  */
  1363.   register struct string_block *to_sb;
  1364.   int pos;
  1365.   int to_pos;
  1366.  
  1367.   to_sb = first_string_block;
  1368.   to_pos = 0;
  1369.  
  1370.   /* Scan each existing string block sequentially, string by string.  */
  1371.   for (from_sb = first_string_block; from_sb; from_sb = from_sb->next)
  1372.     {
  1373.       pos = 0;
  1374.       /* POS is the index of the next string in the block.  */
  1375.       while (pos < from_sb->pos)
  1376.     {
  1377.       register struct Lisp_String *nextstr
  1378.         = (struct Lisp_String *) &from_sb->chars[pos];
  1379.  
  1380.       register struct Lisp_String *newaddr;
  1381.       register int size = nextstr->size;
  1382.  
  1383.       /* NEXTSTR is the old address of the next string.
  1384.          Just skip it if it isn't marked.  */
  1385.       if ((unsigned) size > STRING_BLOCK_SIZE)
  1386.         {
  1387.           /* It is marked, so its size field is really a chain of refs.
  1388.          Find the end of the chain, where the actual size lives.  */
  1389.           while ((unsigned) size > STRING_BLOCK_SIZE)
  1390.         {
  1391.           if (size & 1) size ^= MARKBIT | 1;
  1392.           size = *(int *)size & ~MARKBIT;
  1393.         }
  1394.  
  1395.           total_string_size += size;
  1396.  
  1397.           /* If it won't fit in TO_SB, close it out,
  1398.          and move to the next sb.  Keep doing so until
  1399.          TO_SB reaches a large enough, empty enough string block.
  1400.          We know that TO_SB cannot advance past FROM_SB here
  1401.          since FROM_SB is large enough to contain this string.
  1402.          Any string blocks skipped here
  1403.          will be patched out and freed later.  */
  1404.           while (to_pos + STRING_FULLSIZE (size)
  1405.              > max (to_sb->pos, STRING_BLOCK_SIZE))
  1406.         {
  1407.           to_sb->pos = to_pos;
  1408.           to_sb = to_sb->next;
  1409.           to_pos = 0;
  1410.         }
  1411.           /* Compute new address of this string
  1412.          and update TO_POS for the space being used.  */
  1413.           newaddr = (struct Lisp_String *) &to_sb->chars[to_pos];
  1414.           to_pos += STRING_FULLSIZE (size);
  1415.  
  1416.           /* Copy the string itself to the new place.  */
  1417.           if (nextstr != newaddr)
  1418.         bcopy (nextstr, newaddr, size + 1 + sizeof (int));
  1419.  
  1420.           /* Go through NEXTSTR's chain of references
  1421.          and make each slot in the chain point to
  1422.          the new address of this string.  */
  1423.           size = newaddr->size;
  1424.           while ((unsigned) size > STRING_BLOCK_SIZE)
  1425.         {
  1426.           register Lisp_Object *objptr;
  1427.           if (size & 1) size ^= MARKBIT | 1;
  1428.           objptr = (Lisp_Object *)size;
  1429.  
  1430.           size = XFASTINT (*objptr) & ~MARKBIT;
  1431.           if (XMARKBIT (*objptr))
  1432.             {
  1433.               XSET (*objptr, Lisp_String, newaddr);
  1434.               XMARK (*objptr);
  1435.             }
  1436.           else
  1437.             XSET (*objptr, Lisp_String, newaddr);
  1438.         }
  1439.           /* Store the actual size in the size field.  */
  1440.           newaddr->size = size;
  1441.         }
  1442.       pos += STRING_FULLSIZE (size);
  1443.     }
  1444.     }
  1445.  
  1446.   /* Close out the last string block still used and free any that follow.  */
  1447.   to_sb->pos = to_pos;
  1448.   current_string_block = to_sb;
  1449.  
  1450.   from_sb = to_sb->next;
  1451.   to_sb->next = 0;
  1452.   while (from_sb)
  1453.     {
  1454.       to_sb = from_sb->next;
  1455.       free (from_sb);
  1456.       from_sb = to_sb;
  1457.     }
  1458.  
  1459.   /* Free any empty string blocks further back in the chain.
  1460.      This loop will never free first_string_block, but it is very
  1461.      unlikely that that one will become empty, so why bother checking?  */
  1462.  
  1463.   from_sb = first_string_block;
  1464.   while (to_sb = from_sb->next)
  1465.     {
  1466.       if (to_sb->pos == 0)
  1467.     {
  1468.       if (from_sb->next = to_sb->next)
  1469.         from_sb->next->prev = from_sb;
  1470.       free (to_sb);
  1471.     }
  1472.       else
  1473.     from_sb = to_sb;
  1474.     }
  1475. }
  1476.  
  1477. truncate_all_undos ()
  1478. {
  1479.   register struct buffer *nextb = all_buffers;
  1480.  
  1481.   consing_at_last_truncate = consing_since_gc;
  1482.  
  1483.   while (nextb)
  1484.     {
  1485.       nextb->undo_list 
  1486.     = truncate_undo_list (nextb->undo_list, undo_threshold,
  1487.                   undo_high_threshold);
  1488.       nextb = nextb->next;
  1489.     }
  1490. }
  1491.  
  1492. /* Initialization */
  1493.  
  1494. init_alloc_once ()
  1495. {
  1496.   /* Used to do Vpurify_flag = Qt here, but Qt isn't set up yet!  */
  1497.   pureptr = 0;
  1498.   all_vectors = 0;
  1499.   init_strings ();
  1500.   init_cons ();
  1501.   init_symbol ();
  1502.   init_marker ();
  1503.   gcprolist = 0;
  1504.   staticidx = 0;
  1505.   consing_since_gc = 0;
  1506.   gc_cons_threshold = 100000;
  1507. #ifdef VIRT_ADDR_VARIES
  1508.   malloc_sbrk_unused = 1<<22;    /* A large number */
  1509.   malloc_sbrk_used = 100000;    /* as reasonable as any number */
  1510. #endif /* VIRT_ADDR_VARIES */
  1511. }
  1512.  
  1513. init_alloc ()
  1514. {
  1515.   gcprolist = 0;
  1516. }
  1517.  
  1518. void
  1519. syms_of_alloc ()
  1520. {
  1521.   memory_exhausted_message = Fcons (build_string ("Memory exhausted"), Qnil);
  1522.   staticpro (&memory_exhausted_message);
  1523.  
  1524.   DEFVAR_INT ("gc-cons-threshold", &gc_cons_threshold,
  1525.     "*Number of bytes of consing between garbage collections.");
  1526.  
  1527.   DEFVAR_INT ("pure-bytes-used", &pureptr,
  1528.     "Number of bytes of sharable Lisp data allocated so far.");
  1529.  
  1530. #if 0
  1531.   DEFVAR_INT ("data-bytes-used", &malloc_sbrk_used,
  1532.     "Number of bytes of unshared memory allocated in this session.");
  1533.  
  1534.   DEFVAR_INT ("data-bytes-free", &malloc_sbrk_unused,
  1535.     "Number of bytes of unshared memory remaining available in this session.");
  1536. #endif
  1537.  
  1538.   DEFVAR_LISP ("purify-flag", &Vpurify_flag,
  1539.     "Non-nil means loading Lisp code in order to dump an executable.");
  1540.  
  1541.   DEFVAR_INT ("undo-threshold", &undo_threshold,
  1542.     "Keep no more undo information once it exceeds this size.\n\
  1543. This threshold is applied when garbage collection happens.\n\
  1544. The size is counted as the number of bytes occupied,\n\
  1545. which includes both saved text and other data.");
  1546.   undo_threshold = 15000;
  1547.   DEFVAR_INT ("undo-high-threshold", &undo_high_threshold,
  1548.     "Don't keep more than this much size of undo information.\n\
  1549. A command which pushes past this size is itself forgotten.\n\
  1550. This threshold is applied when garbage collection happens.\n\
  1551. The size is counted as the number of bytes occupied,\n\
  1552. which includes both saved text and other data.");
  1553.   undo_high_threshold = 20000;
  1554.  
  1555.   defsubr (&Scons);
  1556.   defsubr (&Slist);
  1557.   defsubr (&Svector);
  1558.   defsubr (&Smake_list);
  1559.   defsubr (&Smake_vector);
  1560.   defsubr (&Smake_string);
  1561.   defsubr (&Smake_symbol);
  1562.   defsubr (&Smake_marker);
  1563.   defsubr (&Spurecopy);
  1564.   defsubr (&Sgarbage_collect);
  1565. }
  1566.